package com.ysxsoft.wdg.upversion;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


public class MyProgressDialog extends DialogFragment {


    ProgressBar progressBar;
    TextView tvProgress;
    private View mRootView;
    int progress1;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            tvProgress.setText("已下载" + progress1 + "%");
            progressBar.setProgress(progress1);
            if (progress1 == 100) {
                ToastUtils.showToast("下载完成");
                dismiss();
            }
        }
    };

    @Subscribe
    public void getProgressEvent(ProgressEvent progress) {
        progress1 = progress.getProgress();
        handler.sendEmptyMessage(0);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mRootView = View.inflate(getContext(), R.layout.layout_update, null);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawableResource(R.color.bgColor_overlay);
        getDialog().setCanceledOnTouchOutside(false);

        progressBar = mRootView.findViewById(R.id.progressBar);
        tvProgress = mRootView.findViewById(R.id.tv_progress);
        return mRootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStart() {
        super.onStart();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
